<?php
require_once 'common/config.php';
require_once 'common/functions.php';
// 检查登录状态
check_login();
// 获取用户列表
$db = db_connect();
$search = isset($_GET['search']) ? $_GET['search'] : '';
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = 10;
$offset = ($page - 1) * $limit;
// 构建查询条件
$where = "1=1";
if ($search) {
    $where .= " AND (name LIKE :search OR phone LIKE :search OR email LIKE :search)";
}
// 获取总记录数
$countSql = "SELECT COUNT(*) FROM users WHERE {$where}";
$countStmt = $db->prepare($countSql);
if ($search) {
    $countStmt->bindValue(':search', "%{$search}%");
}
$countStmt->execute();
$total = $countStmt->fetchColumn();
$total_pages = ceil($total / $limit);
// 获取当前页数据
$sql = "SELECT * FROM users WHERE {$where} ORDER BY id DESC LIMIT :offset, :limit";
$stmt = $db->prepare($sql);
if ($search) {
    $stmt->bindValue(':search', "%{$search}%");
}
$stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
$stmt->bindValue(':limit', $limit, PDO::PARAM_INT);
$stmt->execute();
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户管理 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="assets/css/dashboard.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>

        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>用户管理</h2>
                </div>
                <div class="header-right">
                    <a href="user_add.php" class="btn btn-primary">
                        <i class="fas fa-plus"></i> 添加用户
                    </a>
                </div>
            </header>

            <div class="content">
                <!-- 搜索和筛选 -->
                <div class="filter-bar">
                    <form action="" method="GET" id="searchForm" class="search-bar">
                        <div class="search-input">
                            <input type="text" name="search" placeholder="搜索用户姓名、手机号..." 
                                   value="<?php echo htmlspecialchars($search); ?>">
                        </div>
                        <button type="submit" class="btn-search">
                            <i class="fas fa-search"></i>
                        </button>
                    </form>
                </div>

                <style>
                .filter-bar {
                    background: #fff;
                    padding: 10px;
                    border-radius: 4px;
                    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
                    margin-bottom: 20px;
                }

                .search-bar {
                    display: flex;
                    margin-top: 12px;
                    align-items: center;
                    gap: 8px;
                }

                .search-input {
                    flex: 1;
                }

                .search-input input {
                    width: 100%;
                    padding: 6px 12px;
                    border: 1px solid #ddd;
                    border-radius: 4px;
                    font-size: 14px;
                    line-height: 1.5;
                    background: #f8f9fa;
                    transition: all 0.3s;
                }

                .search-input input:focus {
                    border-color: #3498db;
                    background: #fff;
                    outline: none;
                }

                .btn-search {
                    padding: 6px 16px;
                    background: #3498db;
                    color: #fff;
                    border: none;
                    border-radius: 4px;
                    cursor: pointer;
                    line-height: 1.5;
                    transition: background 0.3s;
                }

                .btn-search:hover {
                    background: #2980b9;
                }

                /* 响应式布局 */
                @media (max-width: 768px) {
                    .search-bar {
                        flex-direction: column;
                    }
                    
                    .search-input,
                    .btn-search {
                        width: 100%;
                    }
                }
                </style>

                <!-- 数据表格 -->
                <div class="data-table">
                    <table>
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>头像</th>
                                <th>姓名</th>
                                <th>性别</th>
                                <th>年龄</th>
                                <th>手机号</th>
                                <th>邮箱</th>
                                <th>注册时间</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($users as $user): ?>
                            <tr>
                                <td><?php echo $user['id']; ?></td>
                                <td>
                                    <?php if ($user['avatar']): ?>
                                    <img src="../api/user/<?php echo htmlspecialchars($user['avatar']); ?>" class="avatar-small" alt="头像">
                                    <?php else: ?>
                                    <i class="fas fa-user-circle"></i>
                                    <?php endif; ?>
                                </td>
                                <td><?php echo htmlspecialchars($user['name']); ?></td>
                                <td><?php echo $user['gender'] == '1' ? '男' : '女'; ?></td>
                                <td><?php echo $user['age']; ?></td>
                                <td><?php echo htmlspecialchars($user['phone']); ?></td>
                                <td><?php echo htmlspecialchars($user['email']); ?></td>
                                <td><?php echo date('Y-m-d H:i', strtotime($user['created_at'])); ?></td>
                                <td>
                                    <a href="user_edit.php?id=<?php echo $user['id']; ?>" class="btn-icon" title="编辑">
                                        <i class="fas fa-edit"></i>
                                    </a>
                                    <a href="javascript:void(0)" onclick="deleteUser(<?php echo $user['id']; ?>)" class="btn-icon text-danger" title="删除">
                                        <i class="fas fa-trash"></i>
                                    </a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>

                <!-- 分页 -->
                <?php if ($total_pages > 1): ?>
                <div class="pagination">
                    <?php if ($page > 1): ?>
                    <a href="?page=<?php echo ($page-1); ?>&search=<?php echo urlencode($search); ?>">&laquo; 上一页</a>
                    <?php endif; ?>
                    
                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                    <a href="?page=<?php echo $i; ?>&search=<?php echo urlencode($search); ?>" 
                       class="<?php echo $i == $page ? 'active' : ''; ?>">
                        <?php echo $i; ?>
                    </a>
                    <?php endfor; ?>
                    
                    <?php if ($page < $total_pages): ?>
                    <a href="?page=<?php echo ($page+1); ?>&search=<?php echo urlencode($search); ?>">下一页 &raquo;</a>
                    <?php endif; ?>
                </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script>
    function deleteUser(id) {
        if (confirm('确定要删除这个用户吗？此操作不可恢复！')) {
            fetch('user_delete.php', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: 'id=' + id
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('删除成功');
                    location.reload();
                } else {
                    alert(data.message || '删除失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('操作失败，请重试');
            });
        }
    }
    </script>
</body>
</html> 